﻿/*
=======================================================
    ChangeWirerope【换绳】
=======================================================
*/

namespace Services
{
    public interface IChangeWireropeLogic : ILogic<ChangeWireropeModel, ChangeWireropeFilterModel>
    {
    }


    public class ChangeWireropeLogic : BaseLogic<ChangeWireropeModel, ChangeWireropeFilterModel, ChangeWirerope, IChangeWireropeRepository, IChangeWireropeMapping, IChangeWireropeService>, IChangeWireropeLogic
    {
        #region Ioc
        private readonly IUsingWireropeMapping _IUsingWireropeMapping;
        private readonly IUsingWireropeRepository _IUsingWireropeRepository;
        private readonly IStockRepository _IStockRepository;

        private readonly IBillRepository _IBillRepository;

        private readonly IBillDetailRepository _IBillDetailRepository;

        private readonly ICreateCode _ICreateCode;
        private readonly IOperTypeRepository _IOperTypeRepository;
        public ChangeWireropeLogic(ICurrentUser currentUser, IChangeWireropeRepository repository, IChangeWireropeMapping mapping,
            IChangeWireropeService service,
            IUsingWireropeMapping __IUsingWireropeMapping,
            IUsingWireropeRepository __IUsingWireropeRepository,
            IStockRepository __IStockRepository,
            IBillRepository __IBillRepository,
            IBillDetailRepository __IBillDetailRepository, ICreateCode iCreateCode, IOperTypeRepository iOperTypeRepository)
            : base(currentUser, repository, mapping, service)
        {
            _IUsingWireropeMapping = __IUsingWireropeMapping;
            _IUsingWireropeRepository = __IUsingWireropeRepository;

            _IStockRepository = __IStockRepository;

            _IBillRepository = __IBillRepository;

            _IBillDetailRepository = __IBillDetailRepository;
            _ICreateCode = iCreateCode;
            _IOperTypeRepository = iOperTypeRepository;
        }
        #endregion

        #region Insert
        public override ChangeWireropeModel Insert(ChangeWireropeModel model)
        {
            model.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.CreateDate = DateTime.Now;
            model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
            model.Code = _ICreateCode.GetChangeWireropeCode();

            //修改旧绳状态
            var oldInfo = _IUsingWireropeRepository.GetByID(model.OldUsingWireropeID);
            if (oldInfo == null)
            {
                throw new CustomException("该绳子不存在！");
            }

            oldInfo.ChangeDate = DateTime.Now;
            oldInfo.ChangeReason = model.Reason;
            oldInfo.Status = (int)UsingWirerope_Status.作废;
            oldInfo.UpdateDate = DateTime.Now;
            oldInfo.UpdateUserID = _currentUser.CurrentUserInfo.UserID;
            oldInfo.UpdateUserName = _currentUser.CurrentUserInfo.UserName;

            //新建新绳
            model.NewUsingWireropeModel.InstallDate = model.InstallDate;
            model.NewUsingWireropeModel.Status = (int)UsingWirerope_Status.在用;
            model.NewUsingWireropeModel.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            model.NewUsingWireropeModel.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            model.NewUsingWireropeModel.CreateDate = DateTime.Now;
            model.NewUsingWireropeModel.UpdateUserID = _currentUser.CurrentUserInfo.UserID;
            model.NewUsingWireropeModel.UpdateUserName = _currentUser.CurrentUserInfo.UserName;
            model.NewUsingWireropeModel.UpdateDate = DateTime.Now;
            model.NewUsingWireropeModel.GroupInfoID = model.GroupInfoID;

            var stockInfo = _IStockRepository.GetByID(model.NewUsingWireropeModel.StockID.Value);

            if (stockInfo == null)
            {
                throw new CustomException("该绳子不存在！");
            }

            //建立出库单
            //建立BillDetail和Bill数据
            Bill bill = new Bill();
            bill.GroupInfoID = model.GroupInfoID;
            bill.Code = _ICreateCode.GetBillCode(Bill_InOutType.出库);
            bill.BillDate = model.NewUsingWireropeModel.OutStockDate;
            bill.InOutType = (int)Bill_InOutType.出库;

            var where3 = Expressionable.Create<OperType>();
            where3 = where3.And(x => x.GroupInfoID == model.GroupInfoID);
            where3 = where3.And(x => x.IsInit == true);
            where3 = where3.And(x => x.InOutType == (int)Bill_InOutType.出库);
            var temp = _IOperTypeRepository.GetModel(where3);
            if (temp == null)
            {
                throw new CustomException("出入库类型不存在，联系管理员！");
            }

            bill.OperTypeID = temp.OperTypeID;
            bill.WarehouseID = stockInfo.WarehouseID;
            bill.ApplyUserID = _currentUser.CurrentUserInfo.UserID;
            bill.ApplyUserName = _currentUser.CurrentUserInfo.UserName;
            bill.SubmitUserID = _currentUser.CurrentUserInfo.UserID;
            bill.SubmitUserName = _currentUser.CurrentUserInfo.UserName;
            bill.SubmitDate = model.NewUsingWireropeModel.OutStockDate;
            bill.ConfirmUserID = _currentUser.CurrentUserInfo.UserID;
            bill.ConfirmUserName = _currentUser.CurrentUserInfo.UserName;
            bill.ConfirmDate = model.NewUsingWireropeModel.OutStockDate;
            bill.Status = (int)Bill_Status.已确认;
            bill.CreateUserID = _currentUser.CurrentUserInfo.UserID;
            bill.CreateUserName = _currentUser.CurrentUserInfo.UserName;
            bill.CreateDate = model.NewUsingWireropeModel.OutStockDate;


            BillDetail billDetail = new BillDetail();
            billDetail.GroupInfoID = model.GroupInfoID;
            billDetail.Code = stockInfo.Code;
            billDetail.WarehouseID = stockInfo.WarehouseID;
            billDetail.WarehouseName = stockInfo.WarehouseName;
            billDetail.WarehousePositionID = stockInfo.WarehousePositionID;
            billDetail.WarehousePositionName = stockInfo.WarehousePositionName;
            billDetail.GuiGe = model.NewUsingWireropeModel.Code;
            billDetail.Diameter = model.NewUsingWireropeModel.Diameter.Value;
            billDetail.Cnt = model.NewUsingWireropeModel.Cnt.Value;
            billDetail.Name = model.NewUsingWireropeModel.Name;
            billDetail.KeepDay = model.NewUsingWireropeModel.KeepDay;
            billDetail.ChuChang_Files = model.NewUsingWireropeModel.ChuChang_FilesList.GetFileString();
            billDetail.YanShou_Files = model.NewUsingWireropeModel.YanShou_FilesList.GetFileString();
            billDetail.JianCe_Files = model.NewUsingWireropeModel.JianCe_FilesList.GetFileString();
            billDetail.Attr_Files = model.NewUsingWireropeModel.Attr_FilesList.GetFileString();
            billDetail.NianJu = model.NewUsingWireropeModel.NianJu;
            billDetail.MpaID = model.NewUsingWireropeModel.MpaID;
            billDetail.MpaName = model.NewUsingWireropeModel.MpaName;
            billDetail.SurfaceStatusID = model.NewUsingWireropeModel.SurfaceStatusID;
            billDetail.SurfaceStatusName = model.NewUsingWireropeModel.SurfaceStatusName;
            billDetail.TwistingTypeID = model.NewUsingWireropeModel.TwistingTypeID;
            billDetail.TwistingTypeName = model.NewUsingWireropeModel.TwistingTypeName;
            billDetail.LengthNum = model.NewUsingWireropeModel.LengthNum.Value;
            billDetail.SupplierID = model.NewUsingWireropeModel.SupplierID;
            billDetail.SupplierName = model.NewUsingWireropeModel.SupplierName;
            billDetail.ProducerID = model.NewUsingWireropeModel.ProducerID;
            billDetail.ProducerName = model.NewUsingWireropeModel.ProducerName;
            billDetail.DepartmentID = model.NewUsingWireropeModel.DepartmentID;
            billDetail.HoisterID = model.NewUsingWireropeModel.HoisterID;
            billDetail.HoisterName = model.NewUsingWireropeModel.HoisterName;
            billDetail.HoisterUsageID = model.NewUsingWireropeModel.HoisterUsageID;
            billDetail.HoisterUsageName = model.NewUsingWireropeModel.HoisterUsageName;
            billDetail.WireropeTypeID = model.NewUsingWireropeModel.WireropeTypeID;
            billDetail.WireropeTypeName = model.NewUsingWireropeModel.WireropeTypeName;

            stockInfo.IsOut = true;

            _repository.InsertForAdmin(_mapping.Model2Entity(model), bill, billDetail, _IUsingWireropeMapping.Model2Entity(model.NewUsingWireropeModel), oldInfo, stockInfo);

            return model;
        }
        #endregion

        #region GetByID
        public override ChangeWireropeModel GetByID(int id)
        {
            var info = GetModel(new ChangeWireropeFilterModel { _ChangeWireropeID = id });

            var where = Expressionable.Create<UsingWirerope>();
            where = where.And(x => x.UsingWireropeID == info.NewUsingWireropeID);

            info.NewUsingWireropeModel = _IUsingWireropeMapping.Entity2Model(_IUsingWireropeRepository.GetModel(where));

            var where2 = Expressionable.Create<UsingWirerope>();
            where2 = where2.And(x => x.UsingWireropeID == info.OldUsingWireropeID);
            info.OldUsingWireropeModel = _IUsingWireropeMapping.Entity2Model(_IUsingWireropeRepository.GetModel(where2));

            return info;
        }
        #endregion

        #region GetPage
        public override PageModel<ChangeWireropeModel> GetPage(ChangeWireropeFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            PageModel<ChangeWireropeModel> pageModel = new PageModel<ChangeWireropeModel>();

            var (items, totalCount) = _repository.GetPage(_mapping.FilterModel2Where(filterModel), pageIndex, pageSize, orderBy);
            pageModel.TotalCount = totalCount;
            var ItemsTemp = _mapping.Entity2Model(items);

            var ids = ItemsTemp.Select(x => x.OldUsingWireropeID).ToList();

            var where = Expressionable.Create<UsingWirerope>();
            where = where.And(x => ids.Contains(x.UsingWireropeID));
            var list = _IUsingWireropeMapping.Entity2Model(_IUsingWireropeRepository.GetSearch(where));

            foreach (var item in ItemsTemp)
            {
                item.OldUsingWireropeModel = list.FirstOrDefault(x => x.UsingWireropeID == item.OldUsingWireropeID);
            }

            pageModel.Items = ItemsTemp;

            return pageModel;
        }
        #endregion
    }
}
